﻿using System;
using System.Linq;
using System.Web.Routing;
using System.Web;
using System.Web.Mvc;
using Panorama.Data.Identity;

namespace Panorama.Web.Controllers
{
    public abstract class BaseController : Controller
    {
        protected IUowData dataLayer;

        public BaseController() : this(new UowData())
        {
        }

        public BaseController(IUowData data)
        {
            this.dataLayer = data;
        }

        public IUowData DataLayer
        {
            get
            {
                return this.dataLayer;
            }
            set
            {
                this.dataLayer = value;
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // The following code is used for checking if a session has timed out. The default timeout value for ASP.NET is 20mins.
            // The timeout value can be overriden in the Web.config file using the sessionState tag's timeout attribute
            // <sessionState timeout="5"></sessionState>
            // Check for an existing session
            if (null != filterContext.HttpContext.Session)
            {
                // Check if we have a new session
                if (filterContext.HttpContext.Session.IsNewSession)
                {
                    string cookie = filterContext.HttpContext.Request.Headers["Cookie"];
                    // Check if session has timed out
                    if ((null != cookie) && (cookie.IndexOf("ASP.NET_SessionId") >= 0))
                    {
                        HttpContext.GetOwinContext().Authentication.SignOut();

                        string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                        
                        //redirect to login
                        RouteValueDictionary redirectTargetDictionary = new RouteValueDictionary();
                        redirectTargetDictionary.Add("action", "SessionTimeOut");
                        redirectTargetDictionary.Add("controller", controllerName);
                        
                        filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);

                        return;
                    }
                }
            }

            // else continue with action as usual
            base.OnActionExecuting(filterContext);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && this.dataLayer != null)
            {
                this.dataLayer.Dispose();
                this.dataLayer = null;
            }
            base.Dispose(disposing);
        }
        
        #region Helpers
        
        public ActionResult SessionTimeOut()
        {
            //HandleErrorInfo error = new HandleErrorInfo(new Exception(ViewResources.Content.SessionTimeOut), "Session", "Session");
            //if (HttpContext.Request.IsAjaxRequest())
            //{
            //    return RedirectToAction("Home", "Index");
            //}
            return RedirectToAction("Index", "Home");
        }
        
        #endregion
    }
}